package com.dfrz.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dfrz.hospital.pojo.Member;
import com.dfrz.hospital.service.MemberService;
import com.dfrz.hospital.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/member")
public class MemberLoginController {
    @Autowired
    private MemberService memberService;

    /**
     * 医生病人登录
     * @param phone
     * @param password
     * @param type
     * @return
     */
    @RequestMapping("/loginByPatient")
    public R<Member> loginByPatient(String phone, String password, Integer type){
        log.info("电话：" + phone + "-" + "密码：" + password + "-" + type);
        LambdaQueryWrapper<Member> qw = new LambdaQueryWrapper<>();
        if (type == 1){
            qw.eq(Member::getPhone,phone).eq(Member::getCategory,type);
            Member member = memberService.getOne(qw);
            if (member !=null ){
                //md5判断密码
                boolean flag = member.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()));
                if (flag){
                    if (member.getStatus() == 1){
                        return R.success(member);
                    }else {
                        return R.error("违规用户！");
                    }
                }else {

                    return R.error("密码错误，请重试！");
                }
            }else{
                return R.error("用户不存在，请重试！");
            }
        }else {
            return R.error("系统出错，请重试！");
        }
    }
}
